<?php

namespace Bbs\Controller;

use Common\Controller\BaseController;
use Common\Model\AdModel;
use Common\Model\BbsModel;
use Common\Model\TieCountModel;
use Common\Model\AnsModel;
use Common\Model\TieModel;
use Common\Model\UserModel;
use Common\Model\UserOrgModel;

class TieController extends BaseController {

    /**
     * 展示帖子内容
     */
    public function index(){
        $tid = I('get.tid', 0);
        $p = I('get.p', 1);
        $pcount = 20;       // 每页显示数量
        $tie = TieModel::getTieById($tid);
        if (!$tie) {
            $this->error('该话题不存在或已被删除');
        }
        $bbs = BbsModel::getBbsById($tie['bid']);
        if(!$bbs){
            $this->error('社区数据异常，请稍后再试！');
        }
        $list = AnsModel::getAnsList($tid, $p, $pcount);
        $manager = UID ? UserOrgModel::checkManager(UID, $tie['bid']) : false;
        $super = $manager ? UserOrgModel::checkManager(UID, $tie['bid'], true) : false;
        TieCountModel::update($tid, 'visit');
        $this->assign('bbs', $bbs);
        $this->assign('tie', $tie);
        $this->assign('author', UserModel::getUserById($tie['uid'], false));
        $this->assign('count', $list['count']);
        $this->assign('list', $list['list']);
        $this->assign('tmp', date('Ymd', time()));
        $this->assign('ad', AdModel::getBbsImgAd());
        $this->assign('manager', (int)$manager);
        $this->assign('super', (int)$super);
        $this->assign('token', qiniu());
        $this->assign('page', page2($list['count'], $pcount, $p));
        $this->assign('cate', json_decode($bbs['cate'], true));
        $this->assign('sysNotice', AdModel::getSysNotice());
        $this->assign('tieHide', 'tie-hide');
        $this->assign('title', $tie['title'].' - 问答'.($p == 1 ? '' : '[第'.$p.'页]'));
        $this->assign('description', '标题为：'.$tie['title'].'的问答下的'.($p == 1 ? '所有' : '[第'.$p.'页]').'回复，本问答是由用户提交的，所有用户都可以修改并回复，主要用于方便快捷的解决用户提出的问题！'.$bbs['desc']);
        $this->display();
    }

    /**
     * 发布回复【直接发布】
     */
    public function reply(){
        if(!UID){
            $this->error('您需要先登录账号！', U('/login@wooo.wang'));
        }
        $tid = I('get.tid', 0);
        $tie = TieModel::getTieById($tid, false);
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        if(!$tie || !$html){
            $this->error('数据异常，回复失败，请稍后再试！');
        }
        $data = [
            'tid' => $tid,
            'html' => filterXss($html),
            'uid' => UID,
            'create' => time()
        ];
        $result = AnsModel::createAns($tie, $data);
        if(false === $result){
            $this->error('发布回复失败，请稍后再试！');
        }
        $this->success('发布回复成功！', U('/topic/'.$tid));
    }

    /**
     * 使用ajax补充回复
     * @param $aid
     */
    private function patchans($aid){
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        if(!$aid || !$html){
            json(204, '数据异常，补充回复失败');
        }
        $result = AnsModel::publishPatch($aid, $html);
        if(!$result){
            json(204, '补充回复失败');
        }
        json(200, '补充回复成功');
    }

    /**
     * 之后几个ajax方法的公共部分，用以验证权限
     * @param $tid
     * @param null $super
     * @return mixed|null
     */
    private function authAjaxPara($tid, $super = null){
        if(!UID){
            json(204, '请先登陆');
        }
        $tie = null;
        if(!$tid || !$tie = TieModel::getTieById($tid)){
            json(204, '帖子不存在');
        }
        if(!UserOrgModel::checkManager(UID, $tie['oid'], $super)){
            json(204, '没有操作权限');
        }
        return $tie;
    }

    /**
     * ajax 删除帖子
     */
    public function deletetie(){
        $tid = I('post.tid', 0);
        $tie = $this->authAjaxPara($tid);
        if($tie['elite'] == TieModel::ELITE){
            json(204, '无法直接删除精华帖，请先取消精华');
        }
        if($tie['top'] == TieModel::TOP){
            json(204, '无法直接删除顶置帖，请先取消顶置');
        }
        if(TieModel::update($tid, ['status' => TieModel::DELTEL])){
            json(200);
        }
        json(204, '删除帖子失败，请刷新后再试');
    }

    /**
     * 设置与取消设置帖子为精华
     */
    public function toggleElite(){
        $tid = I('post.tid', 0);
        $type = I('post.type', -1);
        if($type != TieModel::NO_ELITE && $type != TieModel::ELITE){
            json(204, '参数错误');
        }
        $this->authAjaxPara($tid, true);
        if(TieModel::update($tid, ['elite' => $type])){
            json(200);
        }
        json(204, '设置帖子失败，请刷新后再试');
    }

    /**
     * 设置与取消设置帖子为顶置
     */
    public function toggleTop(){
        $tid = I('post.tid', 0);
        $type = I('post.type', -1);
        if($type != TieModel::NO_TOP && $type != TieModel::TOP){
            json(204, '参数错误');
        }
        $tie = $this->authAjaxPara($tid, true);
        if($type == TieModel::TOP && TieModel::getTopCount($tie['oid']) >= 3){
            json(204, '最多只能同时设置三个帖子为顶置');
        }
        if(TieModel::update($tid, ['top' => $type])){
            json(200);
        }
        json(204, '设置帖子失败，请刷新后再试');
    }

    /**
     * 删除回复数据
     */
    public function deleteans(){
        $aid = I('post.aid', 0);
        $tid = I('post.tid', 0);
        $this->authAjaxPara($tid);
        if(AnsModel::deleteAns($tid, $aid)){
            json(200);
        }
        json(204, '删除回复失败，请刷新后再试');
    }

}